package cz.oplevka.lahvodroid;

import android.app.Activity;  
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;  
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
  
public class FirstActivity extends Activity  
{

    
    Button myButton;
    public static FirstActivity instance;
    private static String contents;
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.tab_test);
        instance=this;
                
        myButton =(Button)findViewById(R.id.btnSearch);

        myButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent("com.google.zxing.client.android.SCAN");
		      //  intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
		        startActivityForResult(intent, 0);

			}
		});
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {

            	String result =intent.getAction();
                contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

                Toast andEggs = Toast.makeText(FirstActivity.this, getContents(), Toast.LENGTH_SHORT);
    			andEggs.show();
                
                AlertDialog alertDialog = new AlertDialog.Builder(FirstActivity.this).create();
                alertDialog.setTitle("Barcode Results");
                if(Lahvodroid.getXmlBeerList().getBarcode().contains(getContents())){
                    alertDialog.setMessage("Pivo s kodem: "+getContents()+" uz doma je");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
                    });                
                } else {
                    alertDialog.setMessage("Pivo s kodem: "+getContents()+" jeste doma neni");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
                        }
                    });
                    alertDialog.setButton2("Pridej", new DialogInterface.OnClickListener() {
                        @Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(instance, AddActivity.class);                                
                                startActivity(intent);
                        }
                    });
                }
                
                alertDialog.setIcon(R.drawable.icon);
                alertDialog.show();

                // Handle successful scan
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }
    
    /**
     * @return the contents
     */
    public static String getContents() {
        return contents;
    }
}